package com.snail.readyvr.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangyunpeng on 2016/3/10.
 * 一个通用 adapter，适用于只有一种布局的listview和gridview
 * 不需要新建adapter类，只需要在使用的时候new 出CommonAdapter的示例
 * 并传入自己的数据类型即可
 */
public abstract class CommonAdapter<T> extends BaseAdapter {
    protected LayoutInflater mInflater;
    protected Context mContext;
    protected List<T> mDatas;
    protected final int mItemLayoutId;
    private boolean hasRefresh = false;

    public CommonAdapter(Context context, int itemLayoutId) {
        this.mContext = context;
        this.mInflater = LayoutInflater.from(mContext);
        this.mItemLayoutId = itemLayoutId;
        mDatas = new ArrayList<T>();
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }


    public void deleteItem(T t) {
        mDatas.remove(t);
    }

    public void deleteItemByPosition(int position) {
        mDatas.remove(position);
    }

    public void addData(List<T> newDataList, boolean isClaerAll, boolean isRefresh) {
        boolean isNotify = false;
        if (isClaerAll) {
            mDatas.clear();
            mDatas.addAll(newDataList);
            hasRefresh = true;
            isNotify = true;
        } else {
            if (isRefresh) {
                for (T t : newDataList) {
                    if (!mDatas.contains(t)) {
                        mDatas.clear();
                        mDatas.addAll(newDataList);
                        isNotify = true;
                        hasRefresh = true;
                        break;
                    }
                }
            } else {
                hasRefresh = false;
                for (T t : newDataList) {
                    if (!mDatas.contains(t)) {
                        mDatas.add(t);
                        isNotify = true;
                    }
                }
            }
        }
        if (isNotify) {
            notifyDataSetChanged();
        }
    }

    @Override
    public T getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder = getViewHolder(position, convertView, parent);
        convert(viewHolder, getItem(position), position);
        return viewHolder.getConvertView();

    }

    public abstract void convert(ViewHolder helper, T item, int position);

    private ViewHolder getViewHolder(int position, View convertView, ViewGroup parent) {
        return ViewHolder.get(mContext, convertView, parent, mItemLayoutId, position);
    }

    public boolean isHasRefresh() {
        return hasRefresh;
    }

    public void setHasRefresh(boolean hasRefresh) {
        this.hasRefresh = hasRefresh;
    }

    public List<T> getmDatas() {
        return mDatas;
    }

    public void setmDatas(List<T> mDatas) {
        this.mDatas = mDatas;
    }

    public void clearAllData() {
        mDatas.clear();
        notifyDataSetChanged();
    }
}

